package org.example.xiaomi.controller;


import org.apache.commons.beanutils.BeanUtils;
import org.example.xiaomi.dao.impl.CategoryServiceImpl;
import org.example.xiaomi.entity.Category;
import org.example.xiaomi.service.CategoryService;
import org.example.xiaomi.util.HttpResponseUtil;
import org.example.xiaomi.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;




/**
 * 这是增加逻辑
 */
@WebServlet("/category/add")
public class CategoryAddController extends HttpServlet {
    private CategoryService categoryService = new CategoryServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1 获取参数
        Category category = new Category();
        try {
            BeanUtils.populate(category,req.getParameterMap());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        //2 调用业务逻辑
        category.setCreate_time(new Date());
        ResultVO resultVO=categoryService.addCategory(category);
        //3 给出响应
        HttpResponseUtil.response(resp,resultVO);
    }
}
